package cc.mrbird.web.service.impl;

import cc.mrbird.common.service.impl.BaseService;
import cc.mrbird.system.domain.OneStudent;
import cc.mrbird.system.domain.VisitStudent;
import cc.mrbird.web.dao.VisitStudentMapper;
import cc.mrbird.web.domain.RespImport;
import cc.mrbird.web.hanlder.VisitStudentExcelHandler;
import cc.mrbird.web.service.VisitStudentService;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service("visitStudentService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class VisitStudentServiceImpl extends BaseService<VisitStudent> implements VisitStudentService {

    private static final Logger log = LoggerFactory.getLogger(VisitStudentServiceImpl.class);

    @Autowired
    private VisitStudentMapper visitStudentMapper;

    @Override
    public void addVisitStudent(VisitStudent visitStudent, String username) {
        visitStudent.setCreatedTime(new Date());
        visitStudent.setUpdatedTime(new Date());
        visitStudentMapper.insertVisitStudent(visitStudent);
        log.info("插入学生信息成功");
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void updateVisitStudent(VisitStudent visitStudent) {

        visitStudent.setUpdatedTime(new Date());
        visitStudentMapper.updateVisitStudent(visitStudent);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public void deleteVisitStudents(String studentIds) {

        List<String> list = Arrays.asList(studentIds.split(","));
        visitStudentMapper.deleteByIds(list);
    }

    @Override
    public RespImport ImportVisitStudent(MultipartFile file, String username) {

        ImportParams importParams = new ImportParams();      // 数据处理
        IExcelDataHandler<VisitStudent> handler = new VisitStudentExcelHandler();
//        handler.setNeedHandlerFields(new String[] { "考生号" });// 注意这里对应的是excel的列名。也就是对象上指定的列名。
        importParams.setDataHanlder(handler);

        // 需要验证
        importParams.setNeedVerfiy(true);

        List<VisitStudent> successList = null;
        List<VisitStudent> failList = null;
        RespImport respImport = new RespImport();
        try {
            ExcelImportResult<VisitStudent> result = ExcelImportUtil.importExcelMore(file.getInputStream(), VisitStudent.class,
                    importParams);

            successList = result.getList();
            failList = result.getFailList();

            log.info("是否存在验证未通过的数据:" + result.isVerfiyFail());
            log.info("验证通过的数量:" + successList.size());
            log.info("验证未通过的数量:" + failList.size());

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        for ( VisitStudent visitStudent : successList) {
            visitStudent.setUsername(username);
            visitStudent.setCreatedTime(new Date());
            visitStudent.setUpdatedTime(new Date());
            visitStudentMapper.insertVisitStudent(visitStudent);
            log.info("插入学生信息成功");
        }
        respImport.setSuccessNum(successList.size());
        respImport.setFailureNum(failList.size());
        return respImport;
    }

    @Override
    public List<VisitStudent> findVisitStudentAll() {
        return visitStudentMapper.findVisitStudentAll();
    }

    @Override
    public List<VisitStudent> findVisitStudentByCondition(VisitStudent visitStudent) {
        return visitStudentMapper.findVisitStudentByCondition(visitStudent);
    }

    /**
     * 根据id查找考生信息
     * @param studentId
     * @return
     */
    @Override
    public VisitStudent findById(String studentId) {
        return visitStudentMapper.findById(studentId);
    }

    /**
     * 根据 id 查找考生信息
     * @param idNumber
     * @return
     */
    @Override
    public VisitStudent findByIdNumber(String idNumber) {
        return visitStudentMapper.findByIdNumber(idNumber);
    }

}
